package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.MyScoreListAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.ScoreBeanRecord;
import com.lc.luzhengtang.conn.ConnScoreRecord;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/5.
 */
public class MyScoreActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_score)
    TextView textScore;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private static final int TOTAL_COUNTER = 18;

    private static final int PAGE_SIZE = 6;

    MyScoreListAdapter indexAdapter;

    private int mCurrentCounter = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;

    List<ScoreBeanRecord.DataBeanX.DataBean> dataBeanList;

    private int currentPage = 1;
    protected LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myscore);
        ButterKnife.bind(this);

        dataBeanList = new ArrayList<>();
        rvList.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }


    protected void initData() {
        rvList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        rvList.setLayoutManager(new LinearLayoutManager(this));

        indexAdapter = new MyScoreListAdapter(null);
        indexAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        indexAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider);
//        rvList.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 1));

        rvList.setAdapter(indexAdapter);

        swipeLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        swipeLayout.setOnRefreshListener(this);
        indexAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    @OnClick({R.id.img_news, R.id.text_score})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_score:
                break;
        }
    }


    @Override
    public void onRefresh() {
        new ConnScoreRecord(BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<ScoreBeanRecord>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ScoreBeanRecord scoreBeanRecord) throws Exception {
                super.onSuccess(toast, type, o, scoreBeanRecord);
                textScore.setText(String.valueOf(scoreBeanRecord.getZong()));
                List<ScoreBeanRecord.DataBeanX.DataBean> results = scoreBeanRecord.getData().getData();
                if (results != null) {
                    currentPage = 1;
                    indexAdapter.setNewData(results);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                LovelyToast.makeText(MyScoreActivity.this, "网络请求失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                indexAdapter.removeAllFooterView();
                setRefreshing(false);
                indexAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(MyScoreActivity.this, false);
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnScoreRecord(BaseApplication.basePreferences.readUid(), currentPage, new AsyCallBack<ScoreBeanRecord>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                indexAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ScoreBeanRecord scoreBeanRecord) throws Exception {
                super.onSuccess(toast, type, o, scoreBeanRecord);
                textScore.setText(String.valueOf(scoreBeanRecord.getZong()));
                List<ScoreBeanRecord.DataBeanX.DataBean> results = scoreBeanRecord.getData().getData();
                if (results != null && results.size() > 0) {
                    currentPage++;
                    indexAdapter.setNewData(results);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    indexAdapter.loadMoreComplete();
                    View noDataView = inflater.inflate(R.layout.item_no_data, (ViewGroup) rvList.getParent(), false);
                    indexAdapter.addFooterView(noDataView);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                LovelyToast.makeText(MyScoreActivity.this, "网络请求失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                setRefreshing(false);
                indexAdapter.setEnableLoadMore(false);
                LovelyToast.makeText(MyScoreActivity.this, "没有更多数据了", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }


        ).execute(MyScoreActivity.this, false);
    }

    public void setRefreshing(final boolean refreshing) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refreshing);
            }
        });
    }
}
